﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.IO;
using Magnet.Engine.Entities;

namespace Magnet.Engine
{
    /// <summary>
    /// The Level class contains information for a specific Level that is loaded.
    /// It has a background image and reads a text file to create the level.
    /// </summary>
    public class Level
    {
        private String levelName;

        /// <summary>
        /// Loads the passed level and adds it to the World. Note that the World
        /// should clear out all of its entities before loading a level, or they
        /// will all be appended to the list.
        /// 
        /// List of different types:
        /// Start (float)xPos (float)yPos
        /// End (float)xPos (float)yPos
        /// Horseshoe (float)xPos (float)yPos (float)xSize (float) ySize (float)charge (float)rotation
        /// Toggle (float)xPos (float)yPos (float)xSize (float) ySize (float)charge (float)rotation
        /// Polar (float)xPos (float)yPos (float)xSize (float) ySize (float)charge (float)rotation
        /// 
        /// Example:
        /// Start 55.43 100
        /// End 500 200.3
        /// Horseshoe 74 180 50 50 -100 45
        /// 
        /// With a Horseshoe of a Toggle, the sign of the charge parameter determines the initial polarity.
        /// With a Polar magnet, the sign of the charge parameter is ignored.
        /// A rotation parameter of 0 is considered facing straight down. 180 is straight up, etc.
        /// With a Polar magnet, a rotation of 0 means North is facing up and South is facing down.
        /// 
        /// </summary>
        /// <param name="level">The name/file path of the level being loaded.</param>
        public Level(String level,Player player)
        {
            levelName = level;

            LoadContent();

            //Create reader & open file
            TextReader tr = new StreamReader("Content/Levels/" + level + ".txt");

            //Read the text file.
            String line = tr.ReadLine();
            while (line != null)
            {
                //Pull out all the parameters.
                String[] param = line.Split(new char[]{' '});

                //Get the position based on the next two parameters.
                Vector2 position = new Vector2(float.Parse(param[1]),float.Parse(param[2]));

                //Do something based on the first parameter (the type).
                if (param[0].Equals("Horseshoe"))
                {
                    Vector2 size = new Vector2(float.Parse(param[3]), float.Parse(param[4]));
                    float charge = float.Parse(param[5]);
                    float rotation = MathHelper.ToRadians(float.Parse(param[6]));
                    World.AddEntity(new Entities.Magnet(position,MagnetType.Conical,charge,rotation,(float)Math.PI/4,false));
                }
                else if (param[0].Equals("Toggle"))
                {
                    Vector2 size = new Vector2(float.Parse(param[3]), float.Parse(param[4]));
                    float charge = float.Parse(param[5]);
                    float rotation = MathHelper.ToRadians(float.Parse(param[6]));
                    World.AddEntity(new Entities.Magnet(position,MagnetType.Circular,charge,rotation,(float)Math.PI,true));
                }
                else if (param[0].Equals("Polar"))
                {
                    Vector2 size = new Vector2(float.Parse(param[3]), float.Parse(param[4]));
                    float charge = Math.Abs(float.Parse(param[5]));
                    float rotation = MathHelper.ToRadians(float.Parse(param[6]));
                    World.AddEntity(new Entities.Magnet(position,MagnetType.Polar,charge,rotation,(float)Math.PI/3,false));
                }
                else if (param[0].Equals("Start"))
                {
                    player.Position = position;
                    Console.WriteLine("load " + player.Position);
                }
                else if (param[0].Equals("End"))
                {
                    World.AddEntity(new Goal(position));
                }
                else if (param[0].Equals("Debris"))
                {
                    World.AddEntity(new Debris(position));
                }

                line = tr.ReadLine();
            }

            //Close the stream
            tr.Close();
        }

        private Texture2D backgroundImage1;
        private Texture2D backgroundImage2;
        private Texture2D backgroundImage3;
        private Texture2D backgroundImage4;

        public void LoadContent()
        {
            backgroundImage1 = TextureManager.LoadTexture(/*levelName*/"Level1" + "\\water_level_backgroundsky");
            backgroundImage2 = TextureManager.LoadTexture(/*levelName*/"Level1" + "\\water_level_backgroundwater");
            backgroundImage3 = TextureManager.LoadTexture(/*levelName*/"Level1" + "\\water_level_lightwater");
            backgroundImage4 = TextureManager.LoadTexture(/*levelName*/"Level1" + "\\water_level_board");
        }

        public void Draw(SpriteBatch batch)
        {
            batch.Draw(backgroundImage1, new Rectangle(0, 0, 1024, 768),
                new Rectangle(0, 0, 1024, 768), Color.White, 0.0f,
                Vector2.Zero, SpriteEffects.None, 1);

            batch.Draw(backgroundImage2, new Rectangle(0, 0, 1024, 768),
                new Rectangle(0, 0, 1024, 768), Color.White, 0.0f,
                Vector2.Zero, SpriteEffects.None, 1);

            batch.Draw(backgroundImage3, new Rectangle(0, 0, 1024, 768),
                new Rectangle(0, 0, 1024, 768), Color.White, 0.0f,
                Vector2.Zero, SpriteEffects.None, 1);

            batch.Draw(backgroundImage4, new Rectangle(90, 100, 800, 600),
                new Rectangle(0, 0, 800, 600), Color.White, 0.0f,
                Vector2.Zero, SpriteEffects.None, 1);
        }
    }
}
